<?php

/**
 * Description of async_services
 *
 * @author phuchv
 */
require APPPATH . 'libraries/simple_html_dom.php';
require APPPATH . 'libraries/dom_table_extractor.php';

class async_services extends CI_Controller {

    function __construct() {
        $this->_initdata();
    }

    function _initdata() {
        parent::__construct();
        $this->load->helper('eximcached');
    }

    public function sync_weather() {
        $cityList = simplexml_load_file(realpath(APPPATH) . "/data/weather/city.xml");
        $filePath = realpath(APPPATH) . "/data/weather/weather.xml";
        $xml = new DOMDocument();
        $xml_weather = $xml->createElement("weather");
        foreach ($cityList->children() as $city) {
            $arr = explode("|", $city);
            $tempscale = "c";
            $weatherData = json_encode(get_current_weather_data($arr[0], $tempscale, $this->config->base_url()));

            $xml_city = $xml->createElement("city");
            $xml_woeid = $xml->createElement("woeid");
            $woeid = $xml->createTextNode($arr[0]);
            $xml_woeid->appendChild($woeid);

            $xml_data = $xml->createElement("data");
            $data = $xml->createTextNode($weatherData);
            $xml_data->appendChild($data);

            $xml_city->appendChild($xml_woeid);
            $xml_city->appendChild($xml_data);
            $xml_weather->appendChild($xml_city);
        }
        $xml->appendChild($xml_weather);
        $xml->save($filePath);
        remove_cache_weather();
        echo 'Finish weather!';
    }

    public function sync_exchange_rate() {
        $html = file_get_html('http://www.vietcombank.com.vn/exchangerates/ExrateXML.aspx');
        $filePath = realpath(APPPATH) . "/data/exrate/exrate.xml";
        file_put_contents($filePath, $html);
        remove_cache_exchange();
        echo 'Finish exchange_rate!';
    }

    public function sync_gold() {
        //feed SJC
        $filePath = realpath(APPPATH) . "/data/gold/gold_sjc.xml";
        $tableArray = array();
        try {

            $site = "http://118.69.35.146/sjc/";

            // get DOM from URL or file
            $html = file_get_html($site);

            // no data
            if (!$html)
                return;

            $domTableExtractor = new dom_table_extractor(false, false, true);
            $tableArray = $domTableExtractor->GetData($html, '@id="grv_GiaVangUpdate"');
            if ($tableArray == null)
                return;

            $xml = new DOMDocument();
            $xml_goldlist = $xml->createElement("goldlist");
            foreach ($tableArray as $value) {
                $xml_gold = $xml->createElement("gold");

                $xml_province = $xml->createElement("province");
                $province = $xml->createTextNode($value[0]);
                $xml_province->appendChild($province);

                $xml_type = $xml->createElement("type");
                $type = $xml->createTextNode($value[1]);
                $xml_type->appendChild($type);

                $xml_buy = $xml->createElement("buy");
                $buy = $xml->createTextNode($value[2]);
                $xml_buy->appendChild($buy);

                $xml_sell = $xml->createElement("sell");
                $sell = $xml->createTextNode($value[3]);
                $xml_sell->appendChild($sell);

                $xml_gold->appendChild($xml_province);
                $xml_gold->appendChild($xml_type);
                $xml_gold->appendChild($xml_buy);
                $xml_gold->appendChild($xml_sell);

                $xml_goldlist->appendChild($xml_gold);
            }

            $xml->appendChild($xml_goldlist);
            $xml->save($filePath);

            echo 'Finish gold sjc!';
        } catch (Exception $exc) {
            
        }

        //feed Eximbank
        $filePath = realpath(APPPATH) . "/data/gold/gold_eximbank.xml";
        $site = "http://www.eximbank.com.vn/WebsiteExrate1/ExchangeRate_vn.aspx"; // http://www.eximbank.com.vn/WebsiteExRate1/exchange_tuoitre.aspx or http://www.eximbank.com.vn/WebsiteExrate1/exchange.aspx
        // get DOM from URL or file
        $html = file_get_html($site);

        $buy = $html->find('span[id=GoldRateRepeater_ctl01_lblCSHBUYRT]');
        $arr["buy1"] = $buy[0]->innertext;

        $sell = $html->find('span[id=GoldRateRepeater_ctl01_lblCSHSELLRT]');
        $arr["sell1"] = $sell[0]->innertext;

        $buy = $html->find('span[id=GoldRateRepeater_ctl02_lblCSHBUYRT]');
        $arr["buy2"] = $buy[0]->innertext;

        $sell = $html->find('span[id=GoldRateRepeater_ctl02_lblCSHSELLRT]');
        $arr["sell2"] = $sell[0]->innertext;

        $xml = new DOMDocument();
        $xml_goldlist = $xml->createElement("goldlist");
        $xml_gold = $xml->createElement("gold");

        $xml_type = $xml->createElement("type");
        $type = $xml->createTextNode("SJC (10 chỉ)");
        $xml_type->appendChild($type);

        $xml_buy = $xml->createElement("buy");
        $buy = $xml->createTextNode($arr["buy1"]);
        $xml_buy->appendChild($buy);

        $xml_sell = $xml->createElement("sell");
        $sell = $xml->createTextNode($arr["sell1"]);
        $xml_sell->appendChild($sell);

        $xml_gold->appendChild($xml_type);
        $xml_gold->appendChild($xml_buy);
        $xml_gold->appendChild($xml_sell);
        $xml_goldlist->appendChild($xml_gold);

        $xml_gold = $xml->createElement("gold");
        $xml_type = $xml->createElement("type");
        $type = $xml->createTextNode("SJC (1,2,5 chỉ)");
        $xml_type->appendChild($type);

        $xml_buy = $xml->createElement("buy");
        $buy = $xml->createTextNode($arr["buy2"]);
        $xml_buy->appendChild($buy);

        $xml_sell = $xml->createElement("sell");
        $sell = $xml->createTextNode($arr["sell2"]);
        $xml_sell->appendChild($sell);

        $xml_gold->appendChild($xml_type);
        $xml_gold->appendChild($xml_buy);
        $xml_gold->appendChild($xml_sell);
        $xml_goldlist->appendChild($xml_gold);

        $xml->appendChild($xml_goldlist);
        $xml->save($filePath);

        echo 'Finish exim gold!';
    }

}

?>